package main

import (
	"log"
	"math/rand"
)

func main() {
	//makeRandInt_main()
	d := GenerateInt()
	for i := 0; i < 100; i++ {
		log.Printf("index=%d,val=%+v", i, <-d)
	}
}
func GenerateInt() chan int {
	ch := make(chan int, 20)
	go func() {
		for {
			select {
			case ch <- <-GenerateIntA():
			case ch <- <-GenerateIntB():
			}
		}
	}()
	return ch
}
func GenerateIntB() chan int {
	c := make(chan int, 10)
	go func() {
		for {
			c <- rand.Int()
		}
		close(c)
	}()
	return c
}
func GenerateIntA() chan int {
	c := make(chan int, 10)
	go func() {
		for {
			c <- rand.Int()
		}
		close(c)
	}()
	return c
}

//makeRandInt的测试方法
func makeRandInt_main() {
	c := makeRandInt()
	log.Println(<-c)
	log.Println(<-c)
	log.Println(<-c)
}

//该方法一直生产随机的int，存放在chan 中，需要多少去取用多少
func makeRandInt() chan int {
	chInt := make(chan int)
	go func() {
		for {
			chInt <- rand.Int()
		}
		close(chInt)
	}()
	return chInt
}
